package com.bup.gn.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class AppConfig {

    private static final AppConfig config = new AppConfig();

    private Properties properties;

    private AppConfig() {
        properties = new Properties();

        InputStream stream = AppConfig.class
                .getResourceAsStream("/app.properties");

        if (stream == null) {
            throw new IllegalArgumentException(
                    "Could not load app.properties. Please make sure that it is in CLASSPATH.");
        }

        try {
            properties.load(stream);
        } catch (IOException e) {
            IllegalStateException ex = new IllegalStateException(
                    "An error occurred when reading from the input stream");
            ex.initCause(e);
            throw ex;
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                IllegalStateException ex = new IllegalStateException(
                        "An I/O error occured while closing the stream");
                ex.initCause(e);
                throw ex;
            }
        }
    }

    public static AppConfig getInstance() {
        return config;
    }

    public String get(String key) {
        if (properties == null || key == null)
            return null;
        return (String) properties.get(key);
    }
}